
glob_currentCapcha = ''


function intialize(){
	html = '<br> Cards against Humanity Clone <br><br>'
	html += getOptions('createAnswer', 'createAnswer', 220, 56) + '<br>'
	html += getOptions('createQuestion', 'createQuestion', 220, 56) + '<br>'
	html += getOptions('CreateRoom', 'CreateRoom', 220, 56) + '<br>'
	html += getOptions('EnterRoom', 'EnterRoom', 220, 56) + '<br>'
	document.getElementById("contentID").innerHTML = html
}


function EnteringRoom(){

	if(document.getElementById('nameId').value == ''){ alert('You must provide a user name'); return;}
	if(document.getElementById("roomID").value == ''){ alert('You must provide a name for the new room'); return;}
	if(document.getElementById('roomPassword').value == ''){ alert('You must provide a password for the room'); return;}

	var xhr = getAjaxObj()
	cmdStr = 'newRoomName=' + document.getElementById("roomID").value
	cmdStr += '&roomPassword=' + document.getElementById("roomPassword").value
	cmdStr += '&userId=' + document.getElementById("nameId").value
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/enterRoom',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			result = JSON.parse(xhr.responseText)
			if(result['result'] == 'ok')
			{
				window.location.href = '/startGame?room_uuid=' + result['room'] + '&player_uuid=' + result['player']
				return
			}
			else{
				alert(result['msg'])
				return
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 


}

function CreateANewRoom(){

	if(document.getElementById("roomID").value == ''){ alert('You must provide a name for the new room'); return;}
	if(document.getElementById('roomPassword').value == ''){ alert('You must provide a password for the room'); return;}
	if(document.getElementById('capChaID').value == ''){ alert('You cannot enter an empty image code'); return;}

	languageSelect = document.getElementById("languageChosenID")
	language = languageSelect.options[languageSelect.selectedIndex].text
	for(i in language_list){
		if(language_list[i] == language){ language = i; break; }
	}

	var xhr = getAjaxObj()
	cmdStr = 'newRoomName=' + document.getElementById("roomID").value
	cmdStr += '&roomPassword=' + document.getElementById("roomPassword").value
	cmdStr += '&capchaFileName=' + glob_currentCapcha
	cmdStr += '&capchaKey=' + document.getElementById('capChaID').value
	cmdStr += '&language=' + language
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/createNewRoom',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			if(xhr.responseText == 'ok')
			{
				alert('room created, you can now enter the room using the name and password you have created.')
				EnterRoom()
				return
			}
			else{
				alert(xhr.responseText)
				return
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 

}


function createStuff(title, idName, submitFoo){
	html = '<div style="font-size:20px;">'
	
	html += createBackButton()
	html += createInput(title, idName)
	html += createLanguageOptions()
	html += createInput('Proof you are human', 'capChaID')
	html += createCapcha()

	html += '<a href="javascript:' + submitFoo + '()">'
	html += '<img src="img/submitButton.png" width="130px" height="45px" />'
	html += '</a>'
	html += '</div>'
	return html
}

function onSubmitNewAnswer(){
	if(document.getElementById("answerID").value == ''){ alert('You cannot enter an empty answer'); return;}
	if(document.getElementById('capChaID').value == ''){ alert('You cannot enter an empty image code'); return;}

	languageSelect = document.getElementById("languageChosenID")
	language = languageSelect.options[languageSelect.selectedIndex].text
	for(i in language_list){
		if(language_list[i] == language){ language = i; break; }
	}

	var xhr = getAjaxObj()
	cmdStr = 'NewAnswer=' + specialToSafe(document.getElementById("answerID").value)
	cmdStr += '&capchaFileName=' + glob_currentCapcha
	cmdStr += '&capchaKey=' + document.getElementById('capChaID').value
	cmdStr += '&language=' + language
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/submitNewAnswer',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			if(xhr.responseText == 'ok')
			{
				alert('new answer submitted')
				document.getElementById("contentID").innerHTML = createStuff('Create An Answer', 'answerID', 'onSubmitNewAnswer')
				return
			}
			else{
				alert(xhr.responseText)
				return
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 

}

function onSubmitNewQuestion(){

	if(document.getElementById('questionID').value == ''){ alert('You cannot enter an empty question'); return;}
	if(document.getElementById('capChaID').value == ''){ alert('You cannot enter an empty image code'); return;}

	languageSelect = document.getElementById("languageChosenID")
	language = languageSelect.options[languageSelect.selectedIndex].text
	for(i in language_list){
		if(language_list[i] == language){ language = i; break; }
	}

	var xhr = getAjaxObj()
	cmdStr = 'NewAnswer=' + specialToSafe(document.getElementById("questionID").value)
	cmdStr += '&capchaFileName=' + glob_currentCapcha
	cmdStr += '&capchaKey=' + document.getElementById('capChaID').value
	cmdStr += '&language=' + language
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/submitNewQuestion',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			if(xhr.responseText == 'ok')
			{
				alert('new question submitted')
				document.getElementById("contentID").innerHTML = createStuff('Create A Question', 'questionID', 'onSubmitNewQuestion')
				return
			}
			else{
				alert(xhr.responseText)
				return
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 

}

function createAnswer(){
	document.getElementById("contentID").innerHTML = createStuff('Create An Answer', 'answerID', 'onSubmitNewAnswer')
}
function createQuestion(){
	document.getElementById("contentID").innerHTML = createStuff('Create A Question', 'questionID', 'onSubmitNewQuestion')
}

function CreateRoom(){
	html = '<div style="font-size:20px;">'
	
	html += createBackButton()
	html += createInput("New Room Name", "roomID")
	html += createInput("New Room password", "roomPassword")
	html += createLanguageOptions()
	html += createInput('Proof you are human', 'capChaID')
	html += createCapcha()

	html += '<a href="javascript:CreateANewRoom()">'
	html += '<img src="img/CreateRoom.png" width="220px" height="56px" />'
	html += '</a>'
	html += '</div>'
	document.getElementById("contentID").innerHTML = html
}
function EnterRoom(){
	html = '<div style="font-size:20px;">'
	
	html += createBackButton()
	html += createInput("Your name", "nameId")
	html += createInput("Room Name", "roomID")
	html += createInput("Room password", "roomPassword")

	html += '<a href="javascript:EnteringRoom()">'
	html += '<img src="img/EnterRoom.png" width="220px" height="56px" />'
	html += '</a>'
	html += '</div><br><br>'
	document.getElementById("contentID").innerHTML = html

}
